package com.cloud.school.bus.teacherhelper.modules.information.adapter;

import java.util.List;

import com.android.support.jhf.universalimageloader.core.ImageLoader;
import com.android.support.jhf.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.android.support.jhf.utils.DateUtils;
import com.android.support.jhf.utils.ImageUtil;
import com.cloud.school.bus.teacherhelper.R;
import com.cloud.school.bus.teacherhelper.TeacherHelperApplication;
import com.cloud.school.bus.teacherhelper.entitys.Letter;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 系统消息
 * 
 * @author jiahongfei
 * 
 */
public class InformationAdapter extends BaseAdapter {

	private Context mContext;
	private TeacherHelperApplication mApplication;
	private List<Letter> mInformations;
	private String mLastAddTime = "-1";
	
	private OnClickListener mImageOnClickListener;
	private OnClickListener mTextOnClickListener;

	public InformationAdapter(Context context, List<Letter> informations) {
		mContext = context;
		mApplication = (TeacherHelperApplication) mContext
				.getApplicationContext();
		mInformations = informations;
	}

	@Override
	public int getCount() {
		return mInformations.size();
	}

	@Override
	public Object getItem(int arg0) {
		return mInformations.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {

		if (null == arg1) {
			arg1 = LayoutInflater.from(mContext).inflate(
					R.layout.adapter_information, null);
		}

		final Letter letter = mInformations.get(arg0);
		long addtime = 0L;
		try {
			addtime = Long.parseLong(letter.addtime) * 1000;
		} catch (Exception e) {

		}
		ViewGroup dateLayout = (ViewGroup)arg1.findViewById(R.id.dateLayout);
		TextView dateTextView = (TextView) arg1.findViewById(R.id.dateTextView);
//		dateLayout.setVisibility(View.GONE);
//		dateTextView.setVisibility(View.GONE);
		String date = DateUtils.dateFormat(addtime, "yyyy-MM-dd");
//		if ("-1".equals(mLastAddTime) || !date.equals(mLastAddTime)) {
//			dateTextView.setVisibility(View.VISIBLE);
//			dateLayout.setVisibility(View.VISIBLE);
//			dateTextView.setText(date);
//			mLastAddTime = date;
//		}else {
//			dateLayout.setVisibility(View.GONE);
//			dateTextView.setVisibility(View.GONE);
//		}
		if (letter.isShowDate) {
			dateTextView.setVisibility(View.VISIBLE);
			dateLayout.setVisibility(View.VISIBLE);
			dateTextView.setText(date);
			mLastAddTime = date;
		}else {
			dateLayout.setVisibility(View.GONE);
			dateTextView.setVisibility(View.GONE);
		}

		ViewGroup leftLayout = (ViewGroup) arg1.findViewById(R.id.leftLayout);
		TextView leftTextView = (TextView) arg1.findViewById(R.id.leftTextView);
		ImageView leftImageView = (ImageView) arg1
				.findViewById(R.id.leftImageView);

		ViewGroup rightLayout = (ViewGroup) arg1.findViewById(R.id.rightLayout);
		TextView rightTextView = (TextView) arg1
				.findViewById(R.id.rightTextView);
		ImageView rightImageView = (ImageView) arg1
				.findViewById(R.id.rightImageView);

		if ("admin".equals(letter.from_role)) {
			leftLayout.setVisibility(View.VISIBLE);
			rightLayout.setVisibility(View.GONE);

			if ("txt".equals(letter.letter_type)) {
				leftImageView.setVisibility(View.GONE);
				leftTextView.setVisibility(View.VISIBLE);
				leftTextView.setText(letter.content);
				leftLayout.setOnLongClickListener(new OnLongClickListener() {

					@Override
					public boolean onLongClick(View arg0) {
						arg0.setTag(letter.content);
						if(null != mTextOnClickListener){
							mTextOnClickListener.onClick(arg0);
						}
						return false;
					}
				});
			} else if ("img".equals(letter.letter_type)) {
				leftImageView.setVisibility(View.VISIBLE);
				leftTextView.setVisibility(View.GONE);
				ImageLoader.getInstance().displayImage(letter.content,
						leftImageView, mApplication.mCacheDisplayImageOptions,
						new SimpleImageLoadingListener() {
							@Override
							public void onLoadingComplete(String imageUri,
									View view, Bitmap loadedImage) {
								ImageView imageView = (ImageView) view;
//								imageView.setImageBitmap(loadedImage);
								if(imageUri.startsWith("http://")){
									imageView.setImageBitmap(loadedImage);
								}else if(imageUri.startsWith("file:///")){
									ImageUtil.setRotaingImageBitmap(imageUri.replace("file:///", "/"), loadedImage, imageView);
								}else {
									ImageUtil.setRotaingImageBitmap(imageUri, loadedImage, imageView);
								}
							}
						});
				leftImageView.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						arg0.setTag(letter.content);
						if(null != mImageOnClickListener){
							mImageOnClickListener.onClick(arg0);
						}
					}
				});
			}

		} else {
			leftLayout.setVisibility(View.GONE);
			rightLayout.setVisibility(View.VISIBLE);

			if ("txt".equals(letter.letter_type)) {
				rightImageView.setVisibility(View.GONE);
				rightTextView.setVisibility(View.VISIBLE);
				rightTextView.setText(letter.content);
				rightLayout.setOnLongClickListener(new OnLongClickListener() {

					@Override
					public boolean onLongClick(View arg0) {
						arg0.setTag(letter.content);
						if(null != mTextOnClickListener){
							mTextOnClickListener.onClick(arg0);
						}
						return false;
					}
				});
			} else if ("img".equals(letter.letter_type)) {
				rightImageView.setVisibility(View.VISIBLE);
				rightTextView.setVisibility(View.GONE);
				ImageLoader.getInstance().displayImage(letter.content,
						rightImageView, mApplication.mCacheDisplayImageOptions,
						new SimpleImageLoadingListener() {
							@Override
							public void onLoadingComplete(String imageUri,
									View view, Bitmap loadedImage) {
								ImageView imageView = (ImageView) view;
//								imageView.setImageBitmap(loadedImage);
								if(imageUri.startsWith("http://")){
									imageView.setImageBitmap(loadedImage);
								}else if(imageUri.startsWith("file:///")){
									ImageUtil.setRotaingImageBitmap(imageUri.replace("file:///", "/"), loadedImage, imageView);
								}else {
									ImageUtil.setRotaingImageBitmap(imageUri, loadedImage, imageView);
								}
							}
						});
				rightImageView.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						arg0.setTag(letter.content);
						if(null != mImageOnClickListener){
							mImageOnClickListener.onClick(arg0);
						}
					}
				});
			}
		}

		return arg1;
	}
	
	
	public void clearLastAddTime(){
		mLastAddTime = "-1";
	}
	
	/**
	 * 点击image
	 * @param onClickListener
	 */
	public void setImageOnClickListener(OnClickListener onClickListener){
		mImageOnClickListener = onClickListener;
	}
	/**
	 * 点击text
	 * @param onClickListener
	 */
	public void setTextOnClickListener(OnClickListener onClickListener){
		mTextOnClickListener = onClickListener;
	}

}
